package com.bootdo.wx.controller;

import com.bootdo.common.utils.*;
import com.bootdo.wx.domain.GoodsSearchDO;
import com.bootdo.wx.service.GoodsSearchService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * 商品搜索历史表
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2021-05-08 13:46:20
 */
 
@Controller
@RequestMapping("/wx/goodsSearch")
public class GoodsSearchController {
	@Autowired
	private GoodsSearchService goodsSearchService;
	
	@GetMapping()
	@RequiresPermissions("wx:goodsSearch:goodsSearch")
	String GoodsSearch(){
	    return "wx/goodsSearch/goodsSearch";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:goodsSearch:goodsSearch")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&&StringUtils.isNotBlank(params.get("name").toString())){
            List<GoodsSearchDO> goodsSearchList = goodsSearchService.likeList(query);
            int total = goodsSearchList.size();
            PageUtils pageUtils = new PageUtils(goodsSearchList, total);
            return pageUtils;
		}

		List<GoodsSearchDO> goodsSearchList = goodsSearchService.list(query);
		int total = goodsSearchService.count(query);
		PageUtils pageUtils = new PageUtils(goodsSearchList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:goodsSearch:add")
	String add(){
	    return "wx/goodsSearch/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:goodsSearch:edit")
	String edit(@PathVariable("id") String id,Model model){
		GoodsSearchDO goodsSearch = goodsSearchService.get(id);
		model.addAttribute("goodsSearch", goodsSearch);
	    return "wx/goodsSearch/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:goodsSearch:add")
	public R save( GoodsSearchDO goodsSearch){
	    goodsSearch.setId(UuidUtil.get32UUID());
	    goodsSearch.setDelFlag(0);
	    goodsSearch.setGmtCreate(new Date());
		if(goodsSearchService.save(goodsSearch)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:goodsSearch:edit")
	public R update( GoodsSearchDO goodsSearch){
		goodsSearch.setGmtCreate(new Date());
		goodsSearchService.update(goodsSearch);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:goodsSearch:remove")
	public R remove( String id){
		GoodsSearchDO goodsSearch = goodsSearchService.get(id);
		goodsSearch.setDelFlag(1);
		goodsSearch.setGmtCreate(new Date());
		if(goodsSearchService.update(goodsSearch)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:goodsSearch:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				GoodsSearchDO goodsSearch = goodsSearchService.get(string);
				goodsSearch.setDelFlag(1);
				goodsSearch.setGmtCreate(new Date());
				goodsSearchService.update(goodsSearch);
			}
		}
	
		return R.ok();
	}
	
}
